﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ROS.Application.DomainObjects;
using ROS.Persistence;
using ROS.Application.DTO;

namespace ROS.Application
{
    internal class UserService
    {
        #region Fields

        UserDataMapper userDataMapper;

        #endregion

        #region Properties

        #endregion

        #region Constructor

        /// <summary>
        /// Construct a new UserService object.
        /// </summary>
        public UserService()
        {
            userDataMapper = new UserDataMapper();
        }

        #endregion

        #region Methods

        internal ROSStatus AddUser(string firstName, string lastName, bool isManager, out long userID)
        {
            userID = default(long);

            UserType userType = UserType.STAFF;
            if (isManager)
                userType = UserType.MANAGER;

            User newUser = new User(firstName, lastName, userType);

            userDataMapper.Create(newUser);

            userID = newUser.ID;

            return ROSStatus.SUCCESS;
        }

        internal ROSStatus GetUser(long userID, out UserDTO userDTO)
        {
            userDTO = default(UserDTO);
            User user = userDataMapper.FindByID(userID);
            userDTO = mapUserToDTO(user);
            return ROSStatus.SUCCESS;
        }

        internal ROSStatus GetActiveUsers(out IList<UserDTO> userDTOList)
        {
            userDTOList = new List<UserDTO>();

            IList<User> userList = userDataMapper.FindAll();
            foreach (User user in userList)
            {
                if (!user.IsActive)
                    continue;

                UserDTO userDTO = mapUserToDTO(user);
                userDTOList.Add(userDTO);
            }

            return ROSStatus.SUCCESS;
        }

        internal ROSStatus SetUserManagerialStatus(long userID, bool isManager)
        {
            User user = userDataMapper.FindByID(userID);
            user.IsManager = isManager;
            userDataMapper.Update(user);
            return ROSStatus.SUCCESS;
        }

        internal ROSStatus DeleteUser(long userID)
        {
            User user = userDataMapper.FindByID(userID);
            if (user.MayBeDeleted)
            {
                userDataMapper.Delete(user);
            }
            else
            {
                user.Deactivate();
                userDataMapper.Update(user);
            }            
            return ROSStatus.SUCCESS;
        }

        private UserDTO mapUserToDTO(User user)
        {
            UserDTO userDTO = new UserDTO();
            userDTO.ID = user.ID;
            userDTO.FirstName = user.FirstName;
            userDTO.LastName = user.LastName;
            userDTO.IsManager = user.IsManager;
            return userDTO;
        }

        #endregion
    }
}
